 class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    // 100
    public boolean isSameTree(TreeNode p, TreeNode q) {
        ArrayList<Integer> list1=bfs(p);
        ArrayList<Integer> list2=bfs(q);

        if(list1==null && list2==null) return true;
        if(list1!=null && list2==null) return false;
        if(list1==null && list2!=null) return false;
        if(list1.size()!=list2.size()) return false;
        for(int i=0;i<list1.size();i++){
            if(!Objects.equals(list1.get(i), list2.get(i))){
                return false;
            }
        }
//        for(int i=0;i<list1.size();i++){
//            System.out.print(list1.get(i));
//
//        }
//        System.out.println();
//        for(int i=0;i<list2.size();i++){
//            System.out.print(list2.get(i));
//
//        }
//        System.out.println();
        return true;

    }

    public ArrayList<Integer> bfs(TreeNode p) {
        if(p==null) return null;
        // 初始化队列
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        // 将起点加入队列
        queue.offer(p);
        ArrayList<Integer> res = new ArrayList<Integer>();
        while(!queue.isEmpty()){
            int currentLevelSize = queue.size();
            for(int i=1;i<=currentLevelSize;++i){
                TreeNode node=queue.poll();
                res.add(node.val);
                if(node.val!=Integer.MAX_VALUE) {
                    if (node.left == null) {
                        queue.offer(new TreeNode(Integer.MAX_VALUE));
                    } else if (node.left != null) {
                        queue.offer(node.left);
                    }
                    if (node.right == null) {
                        queue.offer(new TreeNode(Integer.MAX_VALUE));
                    } else if (node.right != null) {
                        queue.offer(node.right);
                    }
                }
            }
        }
        return res;
    }
